package com.sunlee.admin.api.module.oss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunlee.admin.api.module.oss.dao.SysOssDao;
import com.sunlee.admin.api.module.oss.entity.SysOssEntity;
import com.sunlee.admin.api.module.oss.service.SysOssService;
import com.sunlee.common.core.constant.Constant;
import com.sunlee.common.core.entity.PageData;
import com.sunlee.common.core.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * 对象存储service
 *
 * @author sunlee
 */
@Service
public class SysOssServiceImpl extends BaseServiceImpl<SysOssDao, SysOssEntity> implements SysOssService {

    @Override
    public PageData<SysOssEntity> page(Map<String, Object> params) {
        IPage<SysOssEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, SysOssEntity.class);
    }

    @Override
    public LambdaQueryWrapper<SysOssEntity> getWrapper(Map<String, Object> params) {
        String name = (String) params.get("name");
        String fileType = (String) params.get("fileType");
        String url = (String) params.get("url");

        return new LambdaQueryWrapper<SysOssEntity>()
                .like(StringUtils.isNotBlank(name), SysOssEntity::getName, name)
                .like(StringUtils.isNotBlank(url), SysOssEntity::getUrl, url)
                .eq(StringUtils.isNotBlank(fileType), SysOssEntity::getFileType, fileType)
                .orderByDesc(SysOssEntity::getCreateDate);
    }
}
